<?php
class Product_categoryModel extends Model {
	private $pagesDB;
	public function __construct(){
		parent::__construct();
		$this->pagesDB = D('Pages');
	}
	protected $_validate = array(
	    array('title','require','请填写标题！'),
    );
	protected $_auto = array(
        array('datetime','time',1,'function'),
		array('datetime','time',2,'function'),
    );
	public function getListToArray($pid=0){
		$re=$this->where('pid='.$pid)->order('sort')->select();
		if($re!==false){
			foreach($re as $k=>$v){
				$re[$k]['sub']=$this->getListToArray($v['id']);
			}
			return $re;
		}
	}
	//获取所有字类,记录类别深度$deep
	public function getListToArray1($pid=0,$siteid=0,$deep=0,$arr=array()){
		$re=$this->where(array('pid'=>$pid,'site_id'=>$siteid))->order('sort')->select();
		if($re!==false){
			$deep++;
			foreach($re as $k=>$v){
				$v['deep']=$deep;
				$arr[]=$v;
				$arr=$this->getListToArray1($v['id'],$siteid,$deep,$arr);
			}
			return $arr;
		}
	}
	
	
	public function getAllIDBypid($pid,$siteid=0,$includeself=true){
		$arr = $this->getListToArray1($pid,$siteid);
		$data = array();
		foreach($arr as $k=>$v){
			$data[]=$v['id']; 
		}
		if($includeself){
			$data[]=$pid;
		}
		return $data;
	}
	
	
	//通过子类ID获取所有父类返回一维数组
	public function getParentCateArr($pid=0, $siteid=0, $arr=array()){
		$re=$this->where(array('id'=>$pid))->find();
		//echo $this->getLastSql();
		if(is_array($re)){
			$arr[]=$re;
			$arr=$this->getParentCateArr($re['pid'],$siteid,$arr);
		}
		return $arr;
	}
	
	public function getParentCate($pid=0, $siteid=0){
		$arr = $this -> getParentCateArr($pid, $siteid);
		
		return array_reverse($arr);
	}
	
	//获取商品类型,返回类型ID
	public function getTypeById($id, $siteid){
		//倒序遍历
		$arr=$this->getParentCate($id,$siteid);
		$type_id=0;
		foreach($arr as $v){
			if($v['type_id']>0){
				$type_id=$v['type_id'];
			}
		}
		return $type_id;
	}
	//获取当前分类对应的可选属性列表
	public function getAttrList($id,$siteid){
		$type_id=$this->getTypeById($id,$siteid);
		$attrModel=M("Attr");
		$attr_valModel=M("Attr_val");
		$attrList=$attrModel->where(array('type_id'=>$type_id,'site_id'=>$siteid,'isFilter'=>1))->order("sort")->select();
		foreach($attrList as $k=>$v){
			$attrList[$k]['list']=$attr_valModel->where(array('attr_id'=>$v['id']))->order('sort')->select();
		}
		return $attrList;
	}
	
	
	
	
	//复制数据
	public function copy($siteid, $tositeid,$map, $pid = 0){
		$re=$this->where($map)->order('sort,id')->select();
		$newsDB = M('Product');
		if($re!==false){
			foreach($re as $k=>$v){
				$v['relate_id'] = $v['id'];
				$map['pid'] = $v['id'];
				$v['site_id'] = $tositeid;
				
				
				//设置页面ID到风格
					$v['page_list_id'] = (int)$this->pagesDB -> where(array('site_id' =>$tositeid, 'rid' => $v['page_list_id'])) -> getField('id');
					$v['page_list_header_id'] = (int)$this->pagesDB -> where(array('site_id' =>$tositeid, 'rid' => $v['page_list_header_id'])) -> getField('id');
					$v['page_list_footer_id'] = (int)$this->pagesDB -> where(array('site_id' =>$tositeid, 'rid' => $v['page_list_footer_id'])) -> getField('id');
					$v['page_view_id'] = (int)$this->pagesDB -> where(array('site_id' =>$tositeid, 'rid' => $v['page_view_id'])) -> getField('id');
					$v['page_view_header_id'] = (int)$this->pagesDB -> where(array('site_id' =>$tositeid, 'rid' => $v['page_view_header_id'])) -> getField('id');
					$v['page_view_footer_id'] = (int)$this->pagesDB -> where(array('site_id' =>$tositeid, 'rid' => $v['page_view_footer_id'])) -> getField('id');
				
				
				
				unset($v['id']);
				$v['pid'] = $pid;
				$pid1 = $this->data($v)->add();
				echo '<script>window.parent.progress("写入一个商品分类，ID为：'.$pid1.'");</script>';
				ob_flush();
                flush();
				//导入产品和新闻 
				if(in_array('Product', $_POST['copydata'])){
					$newsdata = $newsDB -> where(array('category_id' => $map['pid']))-> select();
					foreach($newsdata as $val){
						$val['relate_id'] = $val['id'];
						unset($val['id']);
						
						$val['category_id'] = $pid1;
						$val['site_id'] = $tositeid;
						$news_id = $newsDB->data($val) -> add();
						
						echo '<script>window.parent.progress("写入一个产品，ID为：'.$news_id.'");</script>';
						ob_flush();
                        flush();
					}
				}
				$this->copy($siteid, $tositeid,$map, $pid1);
			}
			return true;
		}
	}
	
	
	public function copy1($map=array('parent_id' => 0), $pid = 0){
		$db = M();
		$re=$db -> table('ecs_category') -> where($map) -> select();dump($re);dump($re);
		$newsDB = M('Product');
		if($re!==false){
			foreach($re as $k=>$v){
				$data['site_id'] = __SITEID__;
				$newsdata = $db -> table('ecs_goods') -> where(array('cat_id' => $v['cat_id']))-> select();
				$map['parent_id'] = $v['cat_id'];
				//unset($v['id']);
				$data['title'] = $v['cat_name'];
				$data['name'] = 'goodscategory'.$v['cat_id'];
				$data['pid'] = $pid;
				$pid1 = $this->data($data)->add();

				//导入产品和新闻
				
				foreach($newsdata as $val){
					//unset($val['id']);
					$gdata['title'] = $val['goods_name'];
					$gdata['img_url'] = $val['original_img'];
					$gdata['price'] = $val['shop_price'];
					$gdata['affmoney'] = $val['const_price'];
					$gdata['category_id'] = $pid1;
					$gdata['site_id'] = __SITEID__;
					$gdata['content'] = $val['goods_desc'];
					$news_id = $newsDB->data($gdata) -> add();
					//dump($news_id);exit;
				}
				$this->copy1($map, $pid1);
			}
			return true;
		}
	}
	
	
}
